package org.scalanative.testsuite.javalib.net

import java.net._

import org.junit.Assert._
import org.junit.Test

import org.scalanative.testsuite.utils.AssertThrows.assertThrows

class URLEncoderTest {
  @Test def nullInputString(): Unit = {
    assertThrows(
      classOf[NullPointerException],
      URLEncoder.encode(null: String, "ignored")
    )
  }

  @Test def nullEncodingName(): Unit = {
    assertThrows(
      classOf[NullPointerException],
      URLEncoder.encode("any", null: String)
    )
  }

  @Test def earlyThrowOfUnsupportedEncodingException(): Unit = {
    assertThrows(
      classOf[java.io.UnsupportedEncodingException],
      URLEncoder.encode("any", "invalid encoding")
    )
  }

  private def assertIsoEncoded(original: String): Unit = {
    // "The recommended encoding scheme to use is UTF-8."
    val encoded = URLEncoder.encode(original, "UTF-8")
    assertTrue(original == encoded)
  }

  @Test def charactersLowerCaseAlphaThroughZuluEncodeTheSame(): Unit = {
    Range('a', 'z') foreach { v => assertIsoEncoded(v.toChar.toString) }
  }

  @Test def charactersUpperCaseAlphaThroughZuluEncodeTheSame(): Unit = {
    Range('A', 'Z') foreach { v => assertIsoEncoded(v.toChar.toString) }
  }

  @Test def characters0Through9EncodeTheSame(): Unit = {
    Range('0', '9') foreach { v => assertIsoEncoded(v.toChar.toString) }
  }

  @Test def specialCharactersDotDashAsteriskUnderscoreEncodeTheSame(): Unit = {
    Seq('.', '-', '*', '_') foreach { v => assertIsoEncoded(v.toChar.toString) }
  }

  @Test def spaceCharacterIsConvertedToThePlusCharacter(): Unit = {
    val inStr = " "
    val outStr = URLEncoder.encode(inStr, "UTF-8")
    assertTrue(outStr == "+")
  }

  @Test def allOtherCharactersAreEncodedUsingPercentEscapes(): Unit = {
    val ex0 = "The string ü@foo-bar"
    val expectedEx0 = "The+string+%C3%BC%40foo-bar"
    val actualEx0 = URLEncoder.encode(ex0, "UTF-8")
    assertTrue(actualEx0 == expectedEx0)
  }
}
